﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.User;
using PetaPoco;

namespace BrnMall.Manager.Manager.User
{
    public static class CashItemManager
    {
        /// <summary>
        /// 添加一条赠送选项
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Add(CashItem model)
        {
            object obj = null;
            using (var db = new QDMallDatabase())
            {
                obj = db.Insert(model);
            }
            if (obj != null)
                return true;
            return false;
        }

        public static bool Update(CashItem model)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Update(model);
            }
            return count > 0 ? true : false;
        }

        public static bool Delete(int id, int storeId)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute("delete cash_cashitem where id=@0 and storeid=@1", id, storeId);
            }
            return count > 0 ? true : false;
        }

        /// <summary>
        /// 获取单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static CashItem Get(int id, int state = 1)
        {
            var sql = "select top 1 * from cash_cashitem where id=@0";
            if (state > 0)
                sql += string.Format(" and state={0}", state);
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<CashItem>(sql, id);
            }
        }
        /// <summary>
        /// 获取赠送金额列表，并按充值金额倒序
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <returns></returns>
        public static List<CashItem> GetListByAmount(int storeId, int pageSize = 50)
        {
            var sql = string.Format("select top {0} * from cash_cashitem where state=1 and storeId={1} order by amount desc", pageSize, storeId);
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<CashItem>(sql);
            }
        }

        /// <summary>
        /// 获取赠送选项列表
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public static List<CashItem> GetList(int storeId, int state, int pageSize = 10)
        {
            string sql = string.Format("select top {0} * from cash_cashitem where storeId={1} and state={2} order by id desc", pageSize, storeId, state);
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<CashItem>(sql);
            }
        }

        /// <summary>
        /// 获取赠送选项列表（分页模式）
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static Page<CashItem> GetListPage(int storeId, int page, int pageSize = 15)
        {
            var sql = new StringBuilder();
            sql.AppendFormat("select * from cash_cashitem where storeId={0}", storeId);
            sql.Append(" order by id desc");

            using (var db = new QDMallDatabase())
            {
                return db.Page<CashItem>(page, pageSize, sql.ToString());
            }
        }
    }
}
